<template>
	<view class="chat">
		<!-- 	<view class="top_box">
			<view class="top_box_flx">
				<u-avatar :src="mood.imgurl" :size="45"></u-avatar>
				<view class="top_box_tetx">
					<view class="">同城货运-司机</view>
					<view class="">{{mood.username}},欢迎您</view>
				</view>
			</view>
		</view> -->
		<Header title="客服中心" />
		<view class="" style="height: 150rpx;"></view>
		<scroll-view :style="{height: `${windowHeight}rpx`}" id="scrollview" scroll-y="true" :scroll-top="scrollTop"
			:scroll-with-animation="true" class="scroll-view">
			<!-- 聊天主体 -->
			<view id="msglistview" class="chat-body">
				<!-- 聊天记录 -->
				<view v-for="(item,index) in msgList" :key="index">
					<!-- 自己发的消息 -->
					<view class="item self" v-if="item.userContent != ''">
						<!-- 文字内容 -->
						<view class="">
							<view class="content right" style="color: #fff;">
								{{item.userContent}}
							</view>
						</view>
					</view>
					<!-- 机器人发的消息 -->
					<view class="item Ai" v-if="item.botContent != '' || keshow">
						<!-- 文字内容 -->
						<view class="">
							
							<view class="content left" v-if="item.botContent">
								{{item.botContent}}
							</view>
							<view class="item_list" v-if="item.list">
								<view class="item_listText">您还可以点击查看以下问题：</view>
								<view class="" v-for="(meth,k) in item.list">
									<view class="item_flex" @click="toMsg(index,meth)">
										<view class="item_dian"></view>
										<view class="item_zi">{{meth.reply}}</view>
									</view>
									<view class="item_line" v-if="k!=item.list.length-1"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<!-- 底部消息发送栏 -->
		<!-- 用来占位，防止聊天消息被发送框遮挡 -->
		<view class="chat-bottom">
			<view class="send-msg">
				<view class="uni-textarea">
					<textarea v-model="chatMsg" maxlength="300" :show-confirm-bar="false" auto-height></textarea>
				</view>
				<button @click="handleSend" class="send-btn">发送</button>
			</view>
		</view>
	</view>
</template>
<script>
	import Header from '@/components/header/header.vue'
	export default {
		data() {
			return {
				//滚动距离
				scrollTop: 0,
				userId: '',
				//发送的消息
				chatMsg: "",
				keshow: false,
				openShow: false,
				mood: {},
				msgMood: {},
				msgMoods: {},
				renq: '',
				msgList: [{
						botContent: "您好，E路同程，很高兴为您服务！",
						list: [{
							reply: '客服热线电话',
							title: '023-88795445'
						}, {
							reply: '人工客服',
							title: '023-88795445'
						}],
						userContent: "",
					},
					{
						botContent: "",
						userContent: "",
					},
				]
			}
		},
		computed: {
			windowHeight() {
				return this.rpxTopx(uni.getSystemInfoSync().windowHeight)
			}
		},
		components: {
			Header
		},
		onShow() {
			this.cread();
			this.msg()
		},
		methods: {
			async cread() {
				let res = await this.$http.driver.getUserInfoDetail();
				if (res.data.data) {
					this.mood = res.data.data
				}
			},
			async msg() {
				let res = await this.$http.users.queryCustomerService()
				this.msgMood = res.data.data
			},
			toMsg(e, i) {
				if (e == 0) {
					uni.makePhoneCall({
						phoneNumber: '023-88795445'
					})
				} else {
					this.chatMsg = i.reply
				}
			},
			// px转换成rpx
			rpxTopx(px) {
				let deviceWidth = wx.getSystemInfoSync().windowWidth
				let rpx = (750 / deviceWidth) * Number(px)
				return Math.floor(rpx)
			},
			async MsgDrilt(e) {
				let res = await this.$http.users.echoCustomerService({
					cid: e
				})
				this.msgMoods = res.data.data
				this.msgMoods.list.forEach((item, i) => {
					item.reply = item.title
				})
				let mood = {
					botContent: "",
					userContent: "",
					list: this.msgMoods.list
				}
				this.msgList.push(mood);
				this.keshow = true
				// this.msgMood.forEach(item=>{
				// 	mood.list.push(item)
				// })
			},

			// 发送消息
			handleSend() {
				//如果消息不为空
				if (!this.chatMsg || !/^\s+$/.test(this.chatMsg)) {
					let cid = ''

					if (this.openShow) {
						this.msgMoods.list.forEach((meth, k) => {
							if (this.chatMsg == meth.reply) {
								let obj = {
									botContent: "",
									userContent: this.chatMsg
								}
								this.msgList.push(obj);
								this.msgList.push({
									botContent: meth.content,
									userContent: "",
									list: ''
								});
							}
						})
						this.renq = JSON.stringify(this.msgMoods)
						if (this.renq.indexOf(this.chatMsg) != -1) {
							this.openShow = false
						}
					} else {
						this.msgList.forEach((item, i) => {
							if (item.list) {
								item.list.forEach((meth, k) => {
									if (this.chatMsg == meth.reply) {
										cid = meth.cid
									}
								})
							}
						})
						this.renq = ''
					}
					if (this.msgList.length < 2) {
						let obj = {
							botContent: "",
							userContent: this.chatMsg
						}
						this.msgList.push(obj);
						let mood = {
							botContent: "hello，请问我有什么可以帮助你的吗？",
							userContent: "",
							list: []
						}
						this.msgList.push(mood);
					} else {
						if (!this.openShow & !this.renq) {
							let obj = {
								botContent: "",
								userContent: this.chatMsg
							}
							this.msgList.push(obj);
						}
						if (cid) {
							this.MsgDrilt(cid)
							this.openShow = true
						} else if (!this.openShow) {
							let mood = {
								botContent: "",
								userContent: "",
								list: []
							}
							//console.log(this.msgMood, 1111111111111)
							this.msgMood.forEach(item => {
								mood.list.push(item)
							})
							this.msgList.push(mood);
							this.keshow = true
							this.openShow = false
						}


					}

					this.chatMsg = '';
				} else {
					this.$modal.showToast('不能发送空白消息')
				}
			},
		}
	}
</script>
<style lang="scss" scoped>
	$chatContentbgc: #171717;
	$sendBtnbgc: #29B0FD;

	view,
	button,
	text,
	input,
	textarea {
		margin: 0;
		padding: 0;
		box-sizing: border-box;
	}

	.item_line {
		width: 540rpx;
		border-bottom: 1rpx solid #EEEEEE;
		margin-top: 20rpx;
	}

	.item_zi {
		font-size: 26rpx;
		color: #0991DF;
		margin-left: 20rpx;
	}

	.item_dian {
		width: 10rpx;
		height: 10rpx;
		border-radius: 100%;
		background-color: #0991DF;
	}

	.item_flex {
		display: flex;
		align-items: center;
		margin-top: 30rpx;
	}

	.item_listText {
		font-size: 28rpx;
		font-weight: 600;
	}

	.item_list {
		width: 620rpx;
		background-color: #fff;
		margin-top: 30rpx;
		border-radius: 10rpx;
		padding: 30rpx;
	}

	.top_box {
		width: 750rpx;
		background-color: #829FAD;
		padding-bottom: 20rpx;

		.top_box_tetx {
			font-size: 28rpx;
			color: #fff;
			margin-left: 30rpx;
		}
	}

	.top_box_flx {
		display: flex;
		align-items: center;
		margin-left: 40rpx;
		padding-top: 20rpx;
	}

	/* 聊天消息 */
	.chat {
		.scroll-view {
			::-webkit-scrollbar {
				display: none;
				width: 0 !important;
				height: 0 !important;
				-webkit-appearance: none;
				background: transparent;
				color: transparent;
			}

			// background-color: orange;
			background-color: #F6F6F6;

			.chat-body {
				display: flex;
				flex-direction: column;
				padding-top: 23rpx;
				// background-color:skyblue;

				.self {
					justify-content: flex-end;
				}

				.item {
					display: flex;
					padding: 23rpx 30rpx;
					// background-color: greenyellow;

					.right {
						background-color: $chatContentbgc;
						color: #fff;
					}

					.left {
						background-color: #FFFFFF;
					}

					// 聊天消息的三角形
					.right::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						left: 100%;
						top: 10px;
						border: 12rpx solid transparent;
						border-left: 12rpx solid $chatContentbgc;
					}

					.left::after {
						position: absolute;
						display: inline-block;
						content: '';
						width: 0;
						height: 0;
						top: 10px;
						right: 100%;
						border: 12rpx solid transparent;
						border-right: 12rpx solid #FFFFFF;
					}

					.content {
						position: relative;
						max-width: 486rpx;
						border-radius: 8rpx;
						word-wrap: break-word;
						padding: 24rpx 24rpx;
						margin: 0 24rpx;
						border-radius: 5px;
						font-size: 32rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #333333;
						line-height: 42rpx;
					}

					.avatar {
						display: flex;
						justify-content: center;
						width: 78rpx;
						height: 78rpx;
						background: $sendBtnbgc;
						border-radius: 8rpx;
						overflow: hidden;

						image {
							align-self: center;
						}

					}
				}
			}
		}

		/* 底部聊天发送栏 */
		.chat-bottom {
			width: 100%;
			height: 177rpx;
			background: #F4F5F7;

			.send-msg {
				display: flex;
				align-items: flex-end;
				padding: 16rpx 30rpx;
				width: 100%;
				min-height: 177rpx;
				position: fixed;
				bottom: 0;
				background: #EDEDED;
			}

			.uni-textarea {
				padding-bottom: 70rpx;

				textarea {
					width: 537rpx;
					min-height: 75rpx;
					max-height: 500rpx;
					background: #FFFFFF;
					border-radius: 8rpx;
					font-size: 32rpx;
					font-family: PingFang SC;
					color: #333333;
					line-height: 43rpx;
					padding: 5rpx 8rpx;
				}
			}

			.send-btn {
				display: flex;
				align-items: center;
				justify-content: center;
				margin-bottom: 70rpx;
				margin-left: 25rpx;
				width: 128rpx;
				height: 75rpx;
				background: $sendBtnbgc;
				border-radius: 8rpx;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #FFFFFF;
				line-height: 28rpx;
			}
		}

	}
</style>